package com.exp.practice.workbench.service.student.impl;

import com.exp.practice.workbench.dao.QuestionDao;
import com.exp.practice.workbench.dao.StudentTestDao;
import com.exp.practice.workbench.domain.Question;
import com.exp.practice.workbench.domain.Student;
import com.exp.practice.workbench.domain.StudentTest;
import com.exp.practice.workbench.service.student.StudentExerciseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentExerciseServiceImpl implements StudentExerciseService {

    @Resource
    private StudentTestDao studentTestDao;

    @Resource
    private QuestionDao questionDao;

    @Override
    public List<StudentTest> getWrongQuestion(String pageNo, String pageSize, String stuId) {
        int pageNos=Integer.valueOf(pageNo);
        int pageSizes=Integer.valueOf(pageSize);
        //记录略过的记录条数
        int skipCount=(pageNos-1)*pageSizes;
        List<StudentTest> list = studentTestDao.getWrongExercisesByStuId(skipCount,pageSizes,stuId);
        return list;
    }

    @Override
    public Map<String, Object> getUndoQuestion(String type, int count, Student student) {
        Map<String,Object> map = new HashMap<>();
        //获得练习过的题目
        List<String> list1 = studentTestDao.getQids(student.getStuId());

        List<Question> list = questionDao.getExcludeQuestions(type, count, list1);
        //将结果加入map中
        map.put("questionList",list);
        return map;
    }

    @Override
    public int addExercise(List<StudentTest> list) {
        if (list!=null && list.size()==1) {
            StudentTest studentTest = list.get(0);
            System.out.println(studentTest);
            boolean exist = studentTestDao.existExercise(studentTest.getStudent().getStuId(), studentTest.getQuestion().getQid());
            if (exist) {
                return studentTestDao.updateExercise(studentTest);
            } else {
                return studentTestDao.insertExercise(studentTest);
            }
        }
        return studentTestDao.addExercise(list);
    }

    @Override
    public List<Question> getQuestion(String qid) {
        return questionDao.getQuestionById(qid);
    }

    @Override
    public List<StudentTest> getHistoryQuestion(String pageNo, String pageSize, String stuId) {
        int pageNos=Integer.valueOf(pageNo);
        int pageSizes=Integer.valueOf(pageSize);
        //记录略过的记录条数
        int skipCount=(pageNos-1)*pageSizes;
        List<StudentTest> list = studentTestDao.getHistoryQuestion(skipCount,pageSizes,stuId);
        return list;
    }

    @Override
    public int getTotalWrong(String stuId) {
        return studentTestDao.getTotalWrong(stuId);
    }

    @Override
    public int getTotalHistory(String stuId) {
        if (stuId!=null && stuId!="") {
            return studentTestDao.getTotalHistory(stuId);
        }
        return 0;
    }
}
